<?php

namespace app\admin\controller\cartoon;

use app\common\controller\Backend;
use fast\Tree;
use think\Cache;
/**
 * 漫画分类
 *
 * @icon fa fa-circle-o
 */
class Cartoonclass extends Backend
{

    /**
     * Cartoonclass模型对象
     * @var \app\admin\model\cartoon\Cartoonclass
     */
    protected $model = null;
    protected $preferlist = [];

//    public function _initialize()
//    {
//        parent::_initialize();
//        $this->model = new \app\admin\model\cartoon\Cartoonclass;
//        $this->view->assign("statusList", $this->model->getStatusList());
//    }
    //改写初始化代码
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\cartoon\Cartoonclass();
        //获取实例tree
        $tree = Tree::instance();
        $this->view->assign("statusList", $this->model->getStatusList());

        /**
         * 初始化方法
         *  * 初始化方法
         * @param array  $arr     2维数组，例如：array(1 => array('id'=>'1','pid'=>0,'name'=>'一级栏目一'),)
         * @param string $pidname 父字段名称
         * @param string $nbsp    空格占位符
         * @return Tree
         * 示例：$tree->init($data, 'pid', '#');
         */
        $tree->init(collection($this->model->order('id desc')->select())->toArray(), 'cartoonclass_id');
        /**
         * [getTreeArray：获取树状数组
         * @param string $myid       要查询的ID
         * @param string $itemprefix 前缀
         * @return array
         * 示例：$treeArr = $tree->getTreeArray(0, '$');
         * 结果返回, 如果前缀和占位符都不穿,spacer为空字符串]
         * [getTreeList：将getTreeArray的结果返回为二维数组
         * @param array  $data
         * @param string $field
         * @return array
         * 示例：$list = $tree->getTreeList($treeArr, 'name')
         * 返回结果]
         */
        $this->preferlist = $tree->getTreeList($tree->getTreeArray(0), 'title');
        $data = [0 => ['type' => 'all', 'name' => __('None')]];
        foreach ($this->preferlist as $k => $v)
        {
            if($v['cartoonclass_id']==0){
                $data[$v['id']] = $v;
            }
        }
        $this->view->assign("parentList", $data);
    }


    /**
     * 查看[改写index方法]
     * @return string|\think\response\Json
     * @throws \think\Exception
     */
    public function index()
    {
        if ($this->request->isAjax())
        {
            $search = $this->request->request("search");
            $type = "all";

            //构造父类select列表选项数据
            $list = [];

            foreach ($this->preferlist as $k => $v)
            {
                if ($search) {
                    if (stripos($v['title'], $search) !== false)
                    {
                        if($type == "all" || $type == null) {
                            $list = $this->preferlist;
                        } else {
                            $list[] = $v;
                        }
                    }
                } else {
                    if($type == "all" || $type == null) {
                        $list = $this->preferlist;
                    }
                }

            }

            $total = count($list);
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }
}
